home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / MPW181-5 / _SETUP.1 / fileobuf.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-21  |  3.2 KB  |  141 lines

  1. /* file_obuffer.cc
  2.  
  3.      Output buffer for unix filesystems written by Tobias Bading
  4.    (bading@cs.tu-berlin.edu)
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24.  
  25. #include <fcntl.h>
  26.  
  27. #ifndef __WIN32__
  28. #include <unistd.h>
  29. #include <sys/ioctl.h>
  30. #else
  31. #include <io.h>
  32. #endif
  33.  
  34. #include <iostream.h>
  35.  
  36. #include "obuffer.h"
  37. #include "header.h"
  38. #ifdef ULAW
  39. #include "ulaw.h"
  40. #endif
  41.  
  42. FileObuffer::FileObuffer (uint32 number_of_channels)
  43. {
  44. #ifdef DEBUG
  45.   if (!number_of_channels || number_of_channels > MAXCHANNELS)
  46.   {
  47.     cerr << "FileObuffer: number of channels has to be in [1, " <<  MAXCHANNELS << "] !\n";
  48.     exit (1);
  49.   }
  50. #endif
  51.  
  52. #ifdef ULAW
  53.   if (number_of_channels > 1)
  54.     cerr << "Are you sure you need stereo u-law output?\n";
  55. #endif
  56.   channels = number_of_channels;
  57.   for (int i = 0; i < number_of_channels; ++i)
  58.     bufferp[i] = buffer + i;
  59. }
  60.  
  61.  
  62. void FileObuffer::append (uint32 channel, int16 value)
  63. {
  64. #ifdef DEBUG
  65.   if (channel >= channels)
  66.   {
  67.     cerr << "illegal channelnumber in FileObuffer::append()!\n";
  68.     exit (1);
  69.   }
  70.   if (bufferp[channel] - buffer >= OBUFFERSIZE)
  71.   {
  72.     cerr << "FileObuffer: buffer overflow!\n";
  73.     exit (1);
  74.   }
  75. #endif
  76.  
  77. #ifdef ULAW
  78.   // convert 16-bit PCM sample to 8-bit ulaw:
  79.   *bufferp[channel] = linear2ulaw[value >> 3];
  80. #else
  81.   *bufferp[channel] = value;
  82. #endif
  83.   bufferp[channel] += channels;
  84. }
  85.  
  86. #ifdef SEEK_STOP
  87. void FileObuffer::clear_buffer(void)
  88. {
  89. }
  90.  
  91. void FileObuffer::set_stop_flag(void)
  92. {
  93. }
  94. #endif // SEEK_STOP
  95.  
  96. void FileObuffer::write_buffer (int fd)
  97. {
  98.   int length = (int)((char *)bufferp[0] - (char *)buffer), writelength;
  99.  
  100.   if ((writelength = write (fd, (char *)buffer, length)) != length)
  101.   {
  102.     // buffer has not been written completely
  103.     if (writelength < 0)
  104.     {
  105.       perror ("write");
  106.       exit (1);
  107.     }
  108.     length -= writelength;
  109.     char *buffer_pos = (char *)buffer;
  110.     do
  111.     {
  112.       buffer_pos += writelength;
  113.       if ((writelength = write (fd, buffer_pos, length)) < 0)
  114.       {
  115.     perror ("write");
  116.     exit (1);
  117.       }
  118.     }
  119.     while (length -= writelength);
  120.   }
  121.  
  122.   for (int i = 0; i < channels; ++i)
  123.     bufferp[i] = buffer + i;
  124. }
  125.  
  126. Obuffer *create_stdout_obuffer(MPEG_Args *maplay_args)
  127. {
  128.     Obuffer *buffer;
  129.  
  130.     enum e_mode mode = maplay_args->MPEGheader->mode();
  131.    enum e_channels which_channels = maplay_args->which_c;
  132.  
  133.    if (mode == single_channel || which_channels != both)
  134.       buffer = new FileObuffer (1);
  135.    else
  136.       buffer = new FileObuffer (2);
  137.  
  138.    return(buffer);
  139. }
  140.  
  141.